package com.kcl.dfss;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.kcl.dfss.http.CameraConnection;
import com.kcl.dfss.http.CameraConnection.ResponseHandler;

public class SetDevicePwdActivity extends Activity implements OnClickListener{

	protected static final String tag = "SetDevicePwdActivity";
	private TextView tv_savedpwd;
	private EditText edit_deviceold,edit_devicenew,edit_devicerepeat;
	private ImageView clear_deviceold,clear_devicenew,clear_devicerepeat;
	private LinearLayout devicepwd_back;
	
	private CameraConnection cameraConnection = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_set_device_pwd);
		
		tv_savedpwd = (TextView) findViewById(R.id.tv_savedpwd);
		edit_deviceold = (EditText) findViewById(R.id.edit_deviceold);
		edit_devicenew = (EditText) findViewById(R.id.edit_devicenew);
		edit_devicerepeat = (EditText) findViewById(R.id.edit_devicerepeat);
		clear_deviceold = (ImageView) findViewById(R.id.clear_deviceold);
		clear_devicenew = (ImageView) findViewById(R.id.clear_devicenew);
		clear_devicerepeat = (ImageView) findViewById(R.id.clear_devicerepeat);
		devicepwd_back = (LinearLayout) findViewById(R.id.devicepwd_back);
		
		cameraConnection = new CameraConnection(new ResponseHandler() {

			@Override
			public void handleResponse(int request_code, String response) {
				Log.d(tag, "修改名称response:"+response);
				Log.d(tag, "修改名称request_code:"+request_code);
				switch(request_code){
				case CameraConnection.CODE_SET_TIME:
					if(response.equals("error")){
						Toast.makeText(SetDevicePwdActivity.this, "修改热点密码失败", Toast.LENGTH_SHORT).show();
						finish();
					}else{
						Toast.makeText(SetDevicePwdActivity.this, "修改热点密码成功，重启设备后生效", Toast.LENGTH_SHORT).show();
						finish();
					}
					break;
				}
			}
		
		}, this);
		
		clear_deviceold.setOnClickListener(this);
		clear_devicenew.setOnClickListener(this);
		clear_devicerepeat.setOnClickListener(this);
		devicepwd_back.setOnClickListener(this);
		tv_savedpwd.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_savedpwd:
			String pwdold = edit_deviceold.getText().toString();
			String pwdnew = edit_devicenew.getText().toString();
			String pwdrepeat = edit_devicerepeat.getText().toString();
			if(pwdold.equals("")){
				showDialog(this, "原密码不能为空");
			}else if(pwdold.length()<8){
				showDialog(this, "原密码不能小于8位");
			}else if(pwdnew.equals("")){
				showDialog(this, "新密码不能为空");
			}else if(pwdnew.length()<8){
				showDialog(this, "新密码不能小于8位");
			}else if(pwdrepeat.equals("")){
				showDialog(this, "确认密码不能为空");
			}else if(pwdrepeat.length()<8){
				showDialog(this, "确认密码不能小于8位");
			}else if(pwdnew.equals(pwdrepeat)){
				cameraConnection.setPassword(pwdrepeat,pwdold);
			}else{
				showDialog(this, "两次输入的密码不同");
			}
			break;
		case R.id.clear_deviceold:
			edit_deviceold.getText().clear();
			break;
		case R.id.clear_devicenew:
			edit_devicenew.getText().clear();
			break;
		case R.id.clear_devicerepeat:
			edit_devicerepeat.getText().clear();
			break;
		case R.id.devicepwd_back:
			finish();
			break;
		default:
			break;
		}
	}
	
	public void showDialog(final Activity context,String message){
		Dialog dialog = new AlertDialog.Builder(context)
        .setTitle("系统提示") 
        .setMessage(message) 
        .setNeutralButton("确定", 
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                	dialog.dismiss();
                }
            })
        .setPositiveButton("取消", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                dialog.dismiss();
            }
        }).create();
    dialog.show(); 
	}
}
